package com.hiultra.archive.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.hiultra.archive.domain.*;
import com.hiultra.archive.mapper.*;
import com.hiultra.archive.service.IArchiveBoxStatusService;
import com.hiultra.common.constant.BusinessConstants;
import com.hiultra.common.exception.ServiceException;
import com.hiultra.common.utils.DateUtils;
import com.hiultra.common.utils.SecurityUtils;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.hiultra.archive.service.IArchiveUploadTaskBoxService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 转存档案盒Service业务层处理
 *
 * @author paidaxing
 * @date 2024-07-24
 */
@Service
public class ArchiveUploadTaskBoxServiceImpl implements IArchiveUploadTaskBoxService {
    @Autowired
    private ArchiveUploadTaskBoxMapper archiveUploadTaskBoxMapper;
    @Autowired
    private ArchiveUploadTaskBagMapper archiveUploadTaskBagMapper;
    @Autowired
    private ArchiveUploadTaskArchiveMapper archiveUploadTaskArchiveMapper;
    @Autowired
    private ArcBoxMapper arcBoxMapper;
    @Autowired
    private ArchiveBoxStatusMapper archiveBoxStatusMapper;
    @Autowired
    private IArchiveBoxStatusService archiveBoxStatusService;
    @Autowired
    private RoomArchiveMapper roomArchiveMapper;
    @Autowired
    private ArchiveUploadTaskMapper archiveUploadTaskMapper;
    @Value("${roomNo}")
    private String roomNo;

    /**
     * 查询转存档案盒
     *
     * @param id 转存档案盒主键
     * @return 转存档案盒
     */
    @Override
    public ArchiveUploadTaskBox selectArchiveUploadTaskBoxById(String id) {
        return archiveUploadTaskBoxMapper.selectArchiveUploadTaskBoxById(id);
    }

    /**
     * 查询转存档案盒列表
     *
     * @param archiveUploadTaskBox 转存档案盒
     * @return 转存档案盒
     */
    @Override
    public List<ArchiveUploadTaskBox> selectArchiveUploadTaskBoxList(ArchiveUploadTaskBox archiveUploadTaskBox) {
        return archiveUploadTaskBoxMapper.selectArchiveUploadTaskBoxList(archiveUploadTaskBox);
    }

    /**
     * 新增转存档案盒
     *
     * @param archiveUploadTaskBox 转存档案盒
     * @return 结果
     */
    @Override
    public int insertArchiveUploadTaskBox(ArchiveUploadTaskBox archiveUploadTaskBox) {
        return archiveUploadTaskBoxMapper.insertArchiveUploadTaskBox(archiveUploadTaskBox);
    }

    /**
     * 修改转存档案盒
     *
     * @param archiveUploadTaskBox 转存档案盒
     * @return 结果
     */
    @Override
    public int updateArchiveUploadTaskBox(ArchiveUploadTaskBox archiveUploadTaskBox) {
        return archiveUploadTaskBoxMapper.updateArchiveUploadTaskBox(archiveUploadTaskBox);
    }

    /**
     * 批量删除转存档案盒
     *
     * @param ids 需要删除的转存档案盒主键
     * @return 结果
     */
    @Override
    public int deleteArchiveUploadTaskBoxByIds(String[] ids) {
        return archiveUploadTaskBoxMapper.deleteArchiveUploadTaskBoxByIds(ids);
    }

    /**
     * 删除转存档案盒信息
     *
     * @param id 转存档案盒主键
     * @return 结果
     */
    @Override
    public int deleteArchiveUploadTaskBoxById(String id) {
        return archiveUploadTaskBoxMapper.deleteArchiveUploadTaskBoxById(id);
    }

    /**
     * 批量修改档案盒转存状态
     *
     * @param ids 需要修改档案盒转存状态主键集合
     * @return 结果
     */
    @Override
    @Transactional
    public int updateArchiveUploadTaskBoxStatus(String taskId, String[] ids, String status) {

        ArchiveUploadTask uploadTask = archiveUploadTaskMapper.selectArchiveUploadTaskByTaskId(taskId);
        //根据状态判断是否具备操作权限02-转入01-转出
        //转入
        if (BusinessConstants.TRANSFERSTATUSIN.equals(status)) {
            //如果是转入
            if (!roomNo.equals(uploadTask.gettransferInRoomNo())) {
                throw new ServiceException("工单编号为" + uploadTask.getTransferNo() + "非入库档案室可操作任务，请检查重试！");
            }
        } else {
            if (!roomNo.equals(uploadTask.getTransferOutRoomNo())) {
                throw new ServiceException("工单编号为" + uploadTask.getTransferNo() + "非出库档案室可操作任务，请检查重试！");
            }
        }
        //档案盒
        String[] idBox = StrUtil.isBlank(ids[2]) ? new String[0] : ids[2].split(";");
        //档案袋
        String[] idBag = StrUtil.isBlank(ids[1]) ? new String[0] : ids[1].split(";");
        //档案
        String[] idArchive = StrUtil.isBlank(ids[0]) ? new String[0] : ids[0].split(";");
        //TODO 二维数组 0 档案； 1 档案袋； 2 档案盒

        int boxOut = 0;
        int boxIn = 0;
        if (idBox.length > 0) {
            archiveUploadTaskBoxMapper.updateArchiveUploadTaskBoxStatus(idBox, status);
            //通过id 查询盒信息
            for (String box : idBox) {
                if (StrUtil.isBlank(box)) {
                    continue;
                }
                ArchiveUploadTaskBox archiveUploadTaskBox = archiveUploadTaskBoxMapper.selectArchiveUploadTaskBoxById(box);
                if (null != archiveUploadTaskBox) {
                    ArcBox arcBox = arcBoxMapper.selectArcBoxByBoxCode(archiveUploadTaskBox.getBoxNo());
                    if (ObjectUtil.isNull(arcBox)) {
                        throw new ServiceException("未查询到档案盒信息，请检查重试！");
                    }
                    //删除档案盒信息
                    arcBox.setDelflag(1);
                    arcBoxMapper.updateArcBox(arcBox);

                    //新增盒状态记录
                    ArchiveBoxStatus archiveBoxStatus = new ArchiveBoxStatus();
                    archiveBoxStatus.setAreaNo(arcBox.getAreaNo());
                    archiveBoxStatus.setCabinetNo(arcBox.getCabinetNo());
                    archiveBoxStatus.setBoxBarCode(archiveUploadTaskBox.getBoxBarCode());
                    archiveBoxStatus.setHouseLocationNo(arcBox.getHouseLocationNo());
                    //根据状态判断是转出还是转入
                    if (BusinessConstants.TRANSFERSTATUSOUT.equals(status)) {
                        archiveBoxStatus.setBoxStatus(BusinessConstants.ARC_OUT);
                    } else {
                        archiveBoxStatus.setBoxStatus(BusinessConstants.ARC_IN);
                    }
                    archiveBoxStatus.setRecordTime(DateUtils.getNowDate());
                    archiveBoxStatus.setPushStatus(BusinessConstants.PUSHSTATUS);
                    archiveBoxStatusMapper.insertArchiveBoxStatus(archiveBoxStatus);
                    //todo 档案室档案盒状态上报
                    //archiveBoxStatusService.reportArchiveBoxStatus(archiveBoxStatus);
                }
            }
        }
        //判断是都结束
        List<ArchiveUploadTaskBox> archiveUploadTaskBoxList = archiveUploadTaskBoxMapper.selectArchiveUploadTaskBoxList(new ArchiveUploadTaskBox());

        for (ArchiveUploadTaskBox archiveUploadTaskBox : archiveUploadTaskBoxList) {

            if (null != archiveUploadTaskBox) {
                if (BusinessConstants.TRANSFERSTATUSOUT.equals(status)) {
                    //如果是转出操作，则判断是否都是转入
                    if (BusinessConstants.PRETRANSFERSTATUSOUT.equals(archiveUploadTaskBox.getStatus())) {
                        boxOut++;
                    }
                } else if (BusinessConstants.TRANSFERSTATUSIN.equals(status)) {
                    //如果是转入操作，则判断是否都是转出
                    if (BusinessConstants.PRETRANSFERSTATUSIN.equals(archiveUploadTaskBox.getStatus())) {
                        boxIn++;
                    }
                }
            }
            if (idBag.length > 0) {
                archiveUploadTaskBagMapper.updateArchiveUploadTaskBagStatus(idBag, status);
            }
            List<ArchiveUploadTaskBag> archiveUploadTaskBagList = archiveUploadTaskBagMapper.selectArchiveUploadTaskBagList(new ArchiveUploadTaskBag() {{
                setTaskId(taskId);
            }});
            //判断是都结束
            for (ArchiveUploadTaskBag archiveUploadTaskBag : archiveUploadTaskBagList) {
                if (ObjectUtil.isAllNotEmpty(archiveUploadTaskBag)) {
                    if (BusinessConstants.TRANSFERSTATUSOUT.equals(status)) {
                        //如果是转出操作，则判断是否都是转入
                        if (BusinessConstants.PRETRANSFERSTATUSOUT.equals(archiveUploadTaskBag.getStatus())) {
                            boxOut++;
                        }
                    } else if (BusinessConstants.TRANSFERSTATUSIN.equals(status)) {
                        //如果是转入操作，则判断是否都是转出
                        if (BusinessConstants.PRETRANSFERSTATUSIN.equals(archiveUploadTaskBag.getStatus())) {
                            boxIn++;
                        }
                    }
                }
            }
            if (idArchive.length > 0) {
                for (String arc : idArchive) {

                    ArchiveUploadTaskArchive archiveUploadTaskArchive = archiveUploadTaskArchiveMapper.selectArchiveUploadTaskArchiveById(arc);
                    //根据档案id查询档案信息
                    if (ObjectUtil.isAllNotEmpty(archiveUploadTaskArchive)) {
                        RoomArchive roomArchive = roomArchiveMapper.selectRoomArchiveById(archiveUploadTaskArchive.getArchiveId());
                        if (ObjectUtil.isNull(roomArchive)) {
                            throw new ServiceException("未查询到档案信息，请检查重试！");
                        }
                        roomArchive.setDelflag("1");
                        roomArchiveMapper.updateRoomArchive(roomArchive);
                    }
                }
                archiveUploadTaskArchiveMapper.updateArchiveUploadTaskArchiveStatus(idArchive, status);

            }
            //判断是都结束
            List<ArchiveUploadTaskArchive> archiveUploadTaskArchiveList = archiveUploadTaskArchiveMapper.selectArchiveUploadTaskArchiveList(new ArchiveUploadTaskArchive() {{
                setTaskId(taskId);
            }});
            for (ArchiveUploadTaskArchive archiveUploadTaskArchive : archiveUploadTaskArchiveList) {
                //查询
                if (ObjectUtil.isAllNotEmpty(archiveUploadTaskArchive)) {
                    if (BusinessConstants.TRANSFERSTATUSOUT.equals(status)) {
                        //如果是转出操作，则判断是否都是转入
                        if (BusinessConstants.PRETRANSFERSTATUSOUT.equals(archiveUploadTaskArchive.getStatus())) {
                            boxOut++;
                        }
                    } else if (BusinessConstants.TRANSFERSTATUSIN.equals(status)) {
                        //如果是转入操作，则判断是否都是转出
                        if (BusinessConstants.PRETRANSFERSTATUSIN.equals(archiveUploadTaskArchive.getStatus())) {
                            boxIn++;
                        }
                    }
                }
            }

            //根据判断任务是否都执行完毕
            ArchiveUploadTask archiveUploadTask = new ArchiveUploadTask();
            archiveUploadTask.setTaskId(taskId);

            //如果是转出且==0
            if (BusinessConstants.TRANSFERSTATUSOUT.equals(status)) {
                if (boxOut == 0) {
                    //修改主表为已出库
                    archiveUploadTask.setTransferStatus(BusinessConstants.TRANSFERSTATUSOUT);
                    archiveUploadTask.setTransferBy(SecurityUtils.getLoginUser().getUser().getCustNo());
                    archiveUploadTask.setTransferTime(DateUtils.getNowDate());
                    archiveUploadTaskMapper.updateArchiveUploadTask(archiveUploadTask);
                }

            }
            if (BusinessConstants.TRANSFERSTATUSIN.equals(status)) {
                if (boxIn == 0) {
                    //修改主表为已入库
                    archiveUploadTask.setTransferStatus(BusinessConstants.TRANSFERSTATUSIN);
                    archiveUploadTask.setTransferBy(SecurityUtils.getLoginUser().getUser().getCustNo());
                    archiveUploadTask.setTransferTime(DateUtils.getNowDate());
                    archiveUploadTaskMapper.updateArchiveUploadTask(archiveUploadTask);
                }
            }
        }
        return 1;
    }
}
